package arrays.二分查找;

//leetcode submit region begin(Prohibit modification and deletion)
class 爱吃香蕉的珂珂875 {
    public int minEatingSpeed(int[] piles, int h) {
        //定义一个函数f(x)，使得在h小时内能吃完，这个x就是最小速度，区间范围是香蕉堆的最多和最少
        int left = 1, right = 1000000000 + 1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (cal(piles, mid) <= h) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        return left;
    }

    public int cal(int[] weights, int mid) {
        //计算最大小时数
        int hour = 0;
        for (int weight : weights) {
            hour += weight / mid;
            if (weight % mid > 0) {
                hour++;
            }
        }
        return hour;
    }
}
//leetcode submit region end(Prohibit modification and deletion)
